/*
 * StringTemplateLayoutView.java
 *
 * Copyright 2010 Jack Leow
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package my.edu.clhs.springframework.web.servlet.view.stringtemplate3;

import my.edu.clhs.stringtemplate.servlet.ServletContextStringTemplateGroupLoader;
import my.edu.clhs.stringtemplate.servlet.StringTemplateSupport;

import org.antlr.stringtemplate.StringTemplate;
import org.antlr.stringtemplate.StringTemplateGroup;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * View using the StringTemplate template engine with separate layout and
 * content templates.
 * 
 * TODO perform design review.
 * TODO add unit tests.
 * 
 * @author Jack Leow
 * @version 1.0
 * @since 1.0 (Mar 6, 2010, 7:09:00 PM)
 */
public class StringTemplateLayoutView extends AbstractStringTemplateView {
    public static final String DEFAULT_LAYOUT_TEMPLATE_PATH =
        "/WEB-INF/stringtemplate/layout";
    
    private static final Log log =
        LogFactory.getLog(StringTemplateLayoutView.class);
    
    private String layoutTemplatePath = DEFAULT_LAYOUT_TEMPLATE_PATH;
    
    protected String getLayoutTemplatePath() {
        return layoutTemplatePath;
    }
    
    public void setLayoutTemplatePath(String layoutTemplatePath) {
        this.layoutTemplatePath = layoutTemplatePath;
    }
    
    protected StringTemplateGroup createTemplateLoader() {
        log.debug("entering createTemplateLoader()");
        // TODO overriding this so that group loader can be registered (once)
        ServletContextStringTemplateGroupLoader templateGroupLoader =
            new ServletContextStringTemplateGroupLoader(
            getTemplateLoaderPath(), getServletContext());
        String encoding = getEncoding();
        if (encoding != null) {
            templateGroupLoader.setFileCharEncoding(encoding);
        }
        
        StringTemplateGroup.registerGroupLoader(templateGroupLoader);
        
        log.debug("exiting createTemplateLoader()");
        return super.createTemplateLoader();
    }
    
    protected StringTemplate loadTemplate(String templateName) {
        log.debug("entering loadTemplate(String)");
        StringTemplate template;
        
        try {
            StringTemplateSupport support = getStringTemplateSupport();
            template = support.loadLayoutTemplate(
                templateName, getLayoutTemplatePath(), getTemplateLoader());
            
            if (template != null) {
                support.appendServletUtilTemplateGroup(template.getGroup());
            }
        } catch (IllegalArgumentException e) {
            if (!e.getMessage().startsWith("Can't find template")) {
                throw e;
            }
            IllegalStateException ise = new IllegalStateException(
                "layout specified by \"layoutTemplatePath\" init-param " +
                "does not exist");
            ise.initCause(e);
            throw ise;
        }
        
        log.debug("exiting loadTemplate(String)");
        return template;
    }
}